package com.js.rtu.fragment.forth;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.js.rtu.R;
import com.js.rtu.adapter.HistoryListAdapter;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.event.StartBrotherEvent;
import com.js.rtu.greendao.DBHelper;
import com.js.rtu.greendao.dao.HistoryBeanDao;
import com.js.rtu.greendao.dao.HistoryInfoBeanDao;
import com.js.rtu.greendao.db.HistoryBean;
import com.js.rtu.greendao.db.HistoryInfoBean;
import com.js.rtu.mvp.bean.Cmd02Bean;
import com.js.rtu.mvp.bean.Cmd92Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H18_Bean;
import com.js.rtu.mvp.bean.base.CommonBean;
import com.js.rtu.mvp.present.Cmd09Presenter;
import com.js.rtu.util.CSVUtil;
import com.js.rtu.util.ExcelUtil;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 历史记录列
 */
public class HistoryListFragment extends BaseFragment {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rcy_list)
    RecyclerView rcyList;
    BasePopupView popupView;

    private HistoryListAdapter mAdapter;

    public static HistoryListFragment newInstance() {
        Bundle args = new Bundle();
        HistoryListFragment fragment = new HistoryListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_forth_history_list;
    }


    @Override
    protected void initView() {
        toolbarTitle.setText("补调记录");
        initToolbarNav(toolbar);
        toolbar.inflateMenu(R.menu.menu_delete);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                DBHelper.getInstance().getSession().getHistoryInfoBeanDao().deleteAll();
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                return false;
            }
        });
        List<HistoryInfoBean> list = DBHelper.getInstance().getSession().getHistoryInfoBeanDao()
                .queryBuilder().orderDesc(HistoryInfoBeanDao.Properties.Id).list();
        mAdapter = new HistoryListAdapter(list);
        initRecyclerView(rcyList, mAdapter, 1);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HistoryInfoBean bean = (HistoryInfoBean) adapter.getData().get(position);
                EventBus.getDefault().post(new StartBrotherEvent(HistoryDetaiFragment.newInstance(bean.getId())));
            }
        });
        mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                HistoryInfoBean bean = (HistoryInfoBean) adapter.getData().get(position);
                popupView = new XPopup.Builder(_mActivity)
                        .asConfirm("删除", "删除该记录？",
                                "取消", "确定",
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        DBHelper.getInstance().getSession().getHistoryInfoBeanDao().delete(bean);
                                        mAdapter.getData().remove(bean);
                                        mAdapter.notifyDataSetChanged();

                                    }
                                }, null, true);

                popupView.show();

                return false;
            }
        });

    }


}
